using Microsoft.EntityFrameworkCore;
using JGSY.CourseManagement.Domain.Entities;
using System.IO;
using System;

namespace JGSY.CourseManagement.Infrastructure.Data.EntityFramework
{
    /// <summary>
    /// SQL Server数据库上下文，用于存储业务数据
    /// </summary>
    public class SqlServerDbContext : DbContext
    {
        /// <summary>
        /// 用户实体集合
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// 课程实体集合
        /// </summary>
        public DbSet<Course> Courses { get; set; }

        /// <summary>
        /// 平台实体集合
        /// </summary>
        public DbSet<Platform> Platforms { get; set; }

        /// <summary>
        /// 播放记录实体集合
        /// </summary>
        public DbSet<PlayRecord> PlayRecords { get; set; }

        /// <summary>
        /// 课程教师关联实体集合
        /// </summary>
        public DbSet<CourseTeacherRelation> CourseTeacherRelations { get; set; }

        /// <summary>
        /// 初始化SqlServerDbContext
        /// </summary>
        /// <param name="options">数据库上下文配置选项</param>
        public SqlServerDbContext(DbContextOptions<SqlServerDbContext> options)
            : base(options)
        {}
        
        /// <summary>
        /// 配置数据库连接
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                // 当未通过依赖注入配置时，可添加默认配置
                optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=JGSYCourseManagement;Trusted_Connection=True;MultipleActiveResultSets=true");
            }
        }

        /// <summary>
        /// 配置实体映射
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置User实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.UserId);
                entity.Property(e => e.UserId)
                    .HasMaxLength(20)
                    .IsRequired()
                    .ValueGeneratedNever()
                    .HasColumnType("nvarchar(20)"); // 可选：明确数据库类型

                entity.Property(e => e.UserName).IsRequired().HasMaxLength(20);
                entity.Property(e => e.RealName).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Password).HasMaxLength(40);
                entity.Property(e => e.IsValidation).IsRequired();
                entity.Property(e => e.IsCanLogin).IsRequired();
                entity.Property(e => e.IsTeacher).IsRequired();
                entity.Property(e => e.Avatar).HasMaxLength(200);
                entity.Property(e => e.Gender);
            });

            // 配置Course实体
            modelBuilder.Entity<Course>(entity =>
            {
                entity.HasKey(e => e.CourseId);
                entity.Property(e => e.CourseId).HasMaxLength(20).IsRequired();
                entity.Property(e => e.CourseName).HasMaxLength(150).IsRequired();
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.IsPublish).IsRequired();
                entity.Property(e => e.CourseCover).HasMaxLength(200).IsRequired();
                entity.Property(e => e.CourseUrl).HasMaxLength(200).IsRequired();
            });

            // 配置Platform实体
            modelBuilder.Entity<Platform>(entity =>
            {
                entity.HasKey(e => e.PlatformId);
                entity.Property(e => e.PlatformId).HasMaxLength(20).IsRequired();
                entity.Property(e => e.PlatformName).HasMaxLength(50).IsRequired();
                entity.Property(e => e.IsValidation).IsRequired();
            });

            // 配置PlayRecord实体（复合主键）
            modelBuilder.Entity<PlayRecord>(entity =>
            {
                entity.HasKey(e => new { e.CourseId, e.PlatformId });
                entity.Property(e => e.CourseId).HasMaxLength(20).IsRequired();
                entity.Property(e => e.PlatformId).HasMaxLength(20).IsRequired();
                entity.Property(e => e.PlayCount).IsRequired();
                entity.Property(e => e.IsGrowing).IsRequired();
                entity.Property(e => e.GrowingRate);
            });

            // 配置CourseTeacherRelation实体（复合主键）
            modelBuilder.Entity<CourseTeacherRelation>(entity =>
            {
                entity.HasKey(e => new { e.CourseId, e.TeacherId });
                entity.Property(e => e.CourseId).HasMaxLength(20).IsRequired();
                entity.Property(e => e.TeacherId).HasMaxLength(20).IsRequired();
            });
        }
    }
}